<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/static/commons/taglibs.jsp"%>
<%@ include file="/static/commons/meta.jsp"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>${title }</title>
<!--自定义index样式-->
<link rel="stylesheet" href="${ctx }/static/styles/basic/basic.css"></link>
<link rel="stylesheet" href="${ctx }/static/styles/edit/tableStyle.css"></link>
<!--js库-->
<%@ include file="/static/commons/jquery-easyui.jsp"%>
<script type="text/javascript" src="${ctx}/static/scripts/set_select.js"></script>
<script type="text/javascript" src="${ctx}/static/scripts/WdatePicker/WdatePicker.js"></script>
<script type="text/javascript">
	var headers = {};
	headers['__RequestVerificationToken'] = "${RequestVerificationToken}";
</script>
<style type="text/css">
body{overflow: hidden; padding: 10px 20px;}
#add-cust{position: relative; top: 4px; right: -8px;}
.table-utd select{width: 64px !important;}
.table-utd tbody td:first-child {width: 20px; text-align: center;}
.table-utd tbody td:last-child {width: 40px; text-align: center;}
.table-utd tfoot{display: none;}
#qjy input[type='text']{height: 100%; }
.gasDiv{width:100px; min-height: 26px; border: 0; font-size: 12px;text-align: left;}
.gasDiv input{width:inherit; height:inherit; cursor: pointer;}
.gasDiv label{margin: 2px; display: inline-block; cursor: pointer; min-width: 43px;}
.gasDiv span{width:100%;display:block; text-align:center; line-height: 26px; height: 26px;}
</style>
</head>

<body>
	<c:set var="disabled" value=""></c:set>
	<c:set var="ifSelect" value=""></c:set>
	<c:set var="isHide" value=""></c:set>
	<c:if test="${not empty view}">
		<c:set var="disabled" value="disabled"></c:set>
		<c:set var="ifSelect" value="false"></c:set>
		<c:set var="isHide" value="hidden"></c:set>
	</c:if>
	<form id="editForm" action="${ctx}/bsOrder/save" method="post">
		<input type="hidden" id="orderId" name="p_orderId" value="${obj.order_id}"></input> 
		<input type="hidden" name="p_orderType" value="1"></input>
		<table class="table-ltr">
			<caption>
				仪器检测接收单
				<label> 序号： ${not empty obj.order_no ? obj.order_no : "系统自动生成"}</label>
				<label> 编号： ${not empty obj.order_id ? obj.order_id : "系统自动生成"}</label>
			</caption>
			<tbody>
				<tr>
					<td class="non-empty">用户单位：</td>
					<td style="width: 190px">
						<vvise:select id="custId" name="p_custId" sqlname="getCust" ifSelect="${ifSelect}" defval="${obj.cust_id}" />
						<div class="btn-add ${empty view?'':'hidden'}" title="添加用户单位" id="add-cust"></div>
					</td>
					<td>队号：</td>
					<td><input type="text" id="teamNo" name="p_teamNo" value="${obj.team_no }" ${disabled } maxLength="50"></input></td>
						
					<td class="non-empty">接收日期：</td>
					<td><input type="text" id="receiveDate" name="p_receiveDate" readonly="readonly" value="<fmt:formatDate value="${obj.receive_date }" pattern="yyyy-MM-dd"/>" lass="Wdate" ${disabled } onFocus="WdatePicker()"></input></td>
					
					<td colspan="2" class="td-empty">&nbsp;</td>
				</tr>
				<tr>
					<td class="non-empty">联系人：</td>
					<td><input type="text" id="linkMan" name="p_linkMan" value="${obj.link_man }" ${disabled } maxLength="10"></input></td>
						
					<td class="non-empty">手机：</td>
					<td><input type="text" id="linkMobile" name="p_linkMobile" value="${obj.link_mobile }" ${disabled } maxLength="11"></input></td>
						
					<td class="non-empty">单位电话：</td>
					<td><input type="text" id="linkPhone" name="p_linkPhone" value="${obj.link_phone }" ${disabled } maxlength="11"></input></td>
						
					<td>传真：</td>
					<td><input type="text" id=linkFax name="p_linkFax" value="${obj.link_fax }" ${disabled } maxlength="11"></input></td>
				</tr>
				<tr>
					<td class="non-empty">收件人：</td>
					<td><input type="text" id="consigneeMan" name="p_consigneeMan" value="${obj.consignee_man }"   ${disabled } maxLength="10"></input></td>
						
					<td class="non-empty">收件人电话：</td>
					<td><input type="text" id="consigneePhone" name="p_consigneePhone" value="${obj.consignee_phone }"  ${disabled } maxlength="11"></input>
					</td>
					
					<td class="non-empty">用户地址：</td>
					<td colspan="3">
						<input type="text" id="address" name="p_address" value="${obj.address }" ${disabled } maxLength="50"></input>
					</td>
				</tr>
				<tr>
					<td class="non-empty">接单人：</td>
					<td>${empty obj.opt_user_ ? LoginUser.userName : obj.opt_user_ }</td>
					
					<td class="non-empty">业务员：</td>
					<td><vvise:select id="serviceUser" name="p_serviceUser" sqlname="getServiceUser" sqlparam="000105" ifSelect="${ifSelect}" defval="${obj.service_user}" className="full_select" /></td>
					
					<td colspan="4" class="td-empty">&nbsp;</td>
				</tr>
			</tbody>
		</table>
		
		<div class="tab">
			<div class="tab-header">
				<ul>
					<li class="s0">气瓶</li>
					<li>气检仪</li>
					<li>空呼</li>
				</ul>
			</div>
			<div class="tab-body">
				<!-- 气瓶 -->
				<div class="tab-content s0">
					<table class="table-utd" id="qp">
						<thead>
							<tr>
								<th>&nbsp;</th>
								<th class="non-empty">名称</th>
								<th class="non-empty">生产厂家</th>
								<th class="non-empty">出厂编号</th>
								<th class="non-empty">容积</th>
								<th class="non-empty">出厂日期</th>
								<th class="non-empty">包装检查</th>
								<th class="non-empty">外观检查</th>
								<th class="non-empty">附件检查</th>
								<th>备注</th>
								<th class="${isHide }"><div class="btn-add" title="添加仪器"></div></th>
							</tr>
						</thead>
						<tbody></tbody>
						<tfoot>
							<tr>
								<td>1</td>
								<td><input type="text" id="deviceName" name="deviceName1" maxlength="32"></input></td>
								<td><input type="text" id="deviceManufactor" name="deviceManufactor1" maxlength="64"></input></td>
								<td><input type="text" id="deivceNo" name="deivceNo1" maxlength="32"></input></td>
								<td><vvise:select id="volumeCategory" name="volumeCategory1" sqlname="getCodeData" sqlparam="volume_category" default="false" /></td>
								<td><input type="text" id="manufactureDate" name="manufactureDate1" readonly="readonly" class="Wdate" onFocus="WdatePicker()"></input></td>
								<td><vvise:select id="packStatus" name="packStatus1" sqlname="getCodeData" sqlparam="is_qualified" default="false"/></td>
								<td><vvise:select id="lookStatus" name="lookStatus1" sqlname="getCodeData" sqlparam="is_qualified" default="false"/></td>
								<td><input type="text" id="fileStatus" name="fileStatus1" maxlength="64"></input></td>
								<td><input type="text" id="remark" name="remark1" maxlength="64"></input></td>
								<td class="${isHide }"><div class="btn-del" title="移除仪器"></div></td>
							</tr>
						</tfoot>
					</table>
				</div>

				<!-- 气检仪 -->
				<div class="tab-content">
					<table class="table-utd" id="qjy">
						<thead>
							<tr>
								<th>&nbsp;</th>
								<th class="non-empty">名称</th>
								<th class="non-empty">生产厂家</th>
								<th class="non-empty">型号</th>
								<th class="non-empty">出厂编号</th>
								<th class="non-empty">气体种类</th>
								<th class="non-empty">包装检查</th>
								<th class="non-empty">外观检查</th>
								<th class="non-empty">附件检查</th>
								<th>备注</th>
								<th class="${isHide }"><div class="btn-add" title="添加仪器"></div></th>
							</tr>
						</thead>
						<tbody></tbody>
						<tfoot>
							<tr>
								<td>1</td>
								<td><input type="text" id="deviceName" name="deviceName2" maxlength="32"></input></td>
								<td><input type="text" id="deviceManufactor" name="deviceManufactor2" maxlength="64"></input></td>
								<td><input type="text" id="deviceModel" name="deviceModel2" maxlength="32"></input></td>
								<td><input type="text" id="deivceNo" name="deivceNo2" maxlength="32"></input></td>
								<td>
									<div id="gasDiv" name="gasDiv" class="gasDiv"><span>/</span></div>
									<vvise:select id="dtHide" name="dtHide" sqlname="getDeviceName" sqlparam="3" default="false" className="hidden"/>
								</td>
								<td><vvise:select id="packStatus" name="packStatus2" sqlname="getCodeData" sqlparam="is_qualified" default="false"/></td>
								<td><vvise:select id="lookStatus" name="lookStatus2" sqlname="getCodeData" sqlparam="is_qualified" default="false"/></td>
								<td><input type="text" id="fileStatus" name="fileStatus2" maxlength="64"></input></td>
								<td><input type="text" id="remark" name="remark2" maxlength="64"></input></td>
								<td class="${isHide }"><div class="btn-del" title="移除仪器"></div></td>
							</tr>
						</tfoot>
					</table>
				</div>

				<!-- 空呼 -->
				<div class="tab-content">
					<table class="table-utd" id="kh">
						<thead>
							<tr>
								<th>&nbsp;</th>
								<th class="non-empty">名称</th>
								<th class="non-empty">生产厂家</th>
								<th class="non-empty">型号</th>
								<th class="non-empty">出厂编号</th>
								<th class="non-empty">包装检查</th>
								<th class="non-empty">外观检查</th>
								<th class="non-empty">附件检查</th>
								<th>备注</th>
								<th class="${isHide }"><div class="btn-add" title="添加仪器"></div></th>
							</tr>
						</thead>
						<tbody></tbody>
						<tfoot>
							<tr>
								<td>1</td>
								<td><input type="text" id="deviceName" name="deviceName3" maxlength="32"></input></td>
								<td><input type="text" id="deviceManufactor" name="deviceManufactor3" maxlength="64"></input></td>
								<td><input type="text" id="deviceModel" name="deviceModel3" maxlength="32"></input></td>
								<td><input type="text" id="deivceNo" name="deivceNo3" maxlength="32"></input></td>
								<td><vvise:select id="packStatus" name="packStatus3" sqlname="getCodeData" sqlparam="is_qualified" default="false"/></td>
								<td><vvise:select id="lookStatus" name="lookStatus3" sqlname="getCodeData" sqlparam="is_qualified" default="false"/></td>
								<td><input type="text" id="fileStatus" name="fileStatus3" maxlength="64"></input></td>
								<td><input type="text" id="remark" name="remark3" maxlength="64"></input></td>
								<td class="${isHide }"><div class="btn-del" title="移除仪器"></div></td>
							</tr>
						</tfoot>
					</table>
				</div>
			</div>
		</div>

	</form>
</body>
<script type="text/javascript">
var wh = $(window).height();
var ht = wh - 20 - 161 - 40 - 5;
$('.tab-body').height(ht);

var Index = function(){
	var table, tbody;
	
	//操作table
	var dynamicTable = function(obj){
		var foot = table.find('tfoot');
		var html = foot.clone(true).html();
		tbody = table.find('tbody');
		tbody.append(html);
		initTdSerial();
	};
	var initTdSerial = function(){
		var tableId =  table[0].id;
		var trs = $(tbody).find('tr');
		for(var i=0, tr; (tr=trs[i])!=null; i++){
			tr.cells[0].innerHTML = i+1;
			if(tableId == 'qjy'){
				$(tr).find(':checkbox').attr('name','gasType2_'+(i+1));
			}
		}
	};
	var bindEvent = function(){
		//tab toggle
		$(document).delegate('.tab-header li', 'click', function(){
			if(!$(this).hasClass('.s0')){
				var $index = $(this).index();
				$(this).siblings('.s0').toggleClass('s0').end().toggleClass('s0');
				$('div.tab-body').find('.s0').toggleClass('s0').end().find('.tab-content:eq('+$index+')').toggleClass('s0');
			}
		});
		//add tr
		$(document).delegate('div.btn-add', 'click', function(){
			table = $(this).parents('.table-utd');
			dynamicTable(this);
		});
		//remove tr
		$(document).delegate('div.btn-del', 'click', function(){
			$(this).parents('tr').remove();
			initTdSerial();
		});
		
		//remove error class
		$(document).delegate('input.error', 'change', function(){
			$(this).removeClass('error');
		});
		
		// 添加用户单位
		$('#add-cust').on('click', function(){
			var pp = parent;
			var custId = '';
			pp.openWindow("客户信息",pp.width2,280,"${ctx}/customer/edit?custId="+custId,[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					saveCustomer();
				}
			},{
				text:'取消',
				iconCls:'icon-no',
				handler:function(){
					pp.closeWindow('winDiv2');
				}
			}],'','winDiv2');
		});
	};
	
	var initGasType = function(){
		var gasHTML = $('#dtHide option').map(function(n){
			var html = '<label><input type="checkbox" name="gasType2" value="'+this.value+'" />'+this.text+'</label>'; 
			return html;
		}).get().join('\n');
		$('#dtHide').siblings('.gasDiv').html(gasHTML);
		$('#dtHide').remove();
	};
	
	
	var saveCustomer = function(){
		var pp = parent;
		var editFrame = pp.winFrame2;
		var custId = editFrame.$('#custId').val();
		var custCode = editFrame.$('#custCode').val();
		
		if(!pp.validData("custCode","请输入客户编码！",'',editFrame))
			return;
			
		if(!pp.validData("custName","请输入客户名称！",'',editFrame))
			return;
			
		var flag = pp.executeAjaxBySync('${ctx}/customer/checkName','custCode='+custCode+"&custId="+custId);
		if(flag == 'error'){
		 	pp.$.messager.alert('操作提示','该客户编码已经存在，请重新输入','warning',function(){
			 	editFrame.$('#custCode').focus();
		 	});
			return;
		}
		
		pp.$.messager.confirm('操作提示','确定要保存吗？',function(r){
		    if (r){
		   	    $.ajax({
					cache: false,
					type: "POST",
					headers: headers,
					url:"${ctx}/customer/save",
					data:editFrame.$('#editForm').serialize(),
					async: false,
				    error: function(request) {
			            pp.$.messager.alert('操作提示',"操作失败！","error");
				    },
				    success: function(data) {
				    	if(data && "success" == data){
				    		pp.$.messager.alert('操作提示',"保存成功！","info",function(){
								changeSelect('${ctx}/common/getSelectOptions?sqlname=getCust','custId');
				    			pp.closeWindow('winDiv2');
				         	});
			         	}else{
			         		pp.$.messager.alert('操作提示',"操作失败！","error");
			         	}
				    }
				});
	         }
	     });
	};
	
	//唯一性验证
	var addError = function(result){
		for (var i = 0, obj; (obj = result[i]) != null; i++) {
    		$("tbody [name^='deivceNo']").each(function(){
    			if(this.value == obj){
    				$(this).addClass('error');
    			}
    		});
    	}
	};
	var uniqueValid = function(){
		$("tbody input[name^='deivceNo'].error").removeClass('error');
		var values = $("tbody input[name^='deivceNo']").map(function(){
			if(this.value){
				return this.value;
			}			
		}).get();
		
	    var result = [], hash = {};
	    for (var i = 0, elem; (elem = values[i]) != null; i++) {
	        if (!hash[elem]) {
	            hash[elem] = true;
	        }else{
	            result.push(elem);
	        }
	    }
	    if(result.length > 0){
	    	$.messager.alert('操作提示','出厂编号不能重复！',"warning");
	    	addError(result);
	    	return false;
	    }
		
	    var pp = parent;
	    var urls = "${ctx}/bsOrder/uniqueDeivceNo";
	    var resultStr = pp.executeAjaxBySync(urls,'orderId=${obj.order_id}&params='+values.toString());
	    if(resultStr!='success'){
	    	$.messager.alert('操作提示','出厂编号：'+resultStr+' 已存在！',"warning");
	    	addError(resultStr.split(','));
	    	return false;
	    }
	    return true;
	};
	
	//有效性验证
	var requiredValid = function(){
		var pp = parent;
		
		if(!pp.validData("custId","请选择用户单位！"))
			return false; 
			
		if(!pp.validData("receiveDate","请选择接收日期！"))
			return false;
			
		if(!pp.validData("linkMan","请输入联系人！"))
			return false;
			
		if(!pp.validData("linkMobile","请输入手机！"))
			return false;
			
		if(!pp.validData("linkPhone","请输入单位电话！"))
			return false;
			
		if(!pp.validData("consigneeMan","请输入收件人！"))
			return false;
			
		if(!pp.validData("consigneePhone","请输入收件人电话！"))
			return false;
			
		if(!pp.validData("address","请输入用户地址！"))
			return false;
			
		if(!pp.validData("serviceUser","请选择业务员！"))
			return false;

		
		var equTR = $('.table-utd tbody tr');
		if(equTR.length == 0){
			pp.alertMsg('请添加待检测仪器！','warning');
			return false;
		}
		
		//气瓶
		var qpTR = $('#qp tbody tr');
		var qpTips = ['名称', '生产厂家', '出厂编号', '出厂日期', '附件检查'];
		for(var i=0, tr; (tr=qpTR[i])!=null; i++){
			var texts = $(tr).find(':text');
			for(var a=0, td; (obj=texts[a])!=null; a++){
				var name = obj.name,
					val = obj.value;
				if(name=='remark1'){continue;}
				if(pp.trim(val) == ""){
					if(!$('.tab-header li:eq(0)').hasClass('s0')){
						$('.tab-header li:eq(0)').click();
					}
					$.messager.alert('操作提示','第【'+ (i+1) +'】行'+qpTips[a]+'不能为空！',"warning",function(){
					 	obj.focus();
					});
					return false;
				}
			}
		}
		
		//气检仪
		var qjyTR = $('#qjy tbody tr');
		var qjyTips = ['名称', '生产厂家', '型号', '出厂编号', '附件检查'];
		for(var i=0, tr; (tr=qjyTR[i])!=null; i++){
			var texts = $(tr).find(':text, :checkbox:checked');
			var num = 0;
			for(var a=0, td; (obj=texts[a])!=null; a++){
				var name = obj.name,
					val = obj.value;
				if(name=='remark2'){continue;}
				if(name.indexOf('gasType2')!=-1){num++; continue;}
				if(texts.length==7 && a==4){
					if(!$('.tab-header li:eq(1)').hasClass('s0')){
						$('.tab-header li:eq(1)').click();
					}
					$.messager.alert('操作提示','请选择第【'+ (i+1) +'】行气体种类！',"warning");
					return false;
				}
				if(pp.trim(val) == ""){
					if(!$('.tab-header li:eq(1)').hasClass('s0')){
						$('.tab-header li:eq(1)').click();
					}
					$.messager.alert('操作提示','第【'+ (i+1) +'】行'+qjyTips[a-num]+'不能为空！',"warning",function(){
					 	obj.focus();
					});
					return false;
				}
			}
		}
		
		//空呼
		var khTR = $('#kh tbody tr');
		var khTips = ['名称', '生产厂家', '型号', '出厂编号', '附件检查'];
		for(var i=0, tr; (tr=khTR[i])!=null; i++){
			var texts = $(tr).find(':text');
			for(var a=0, td; (obj=texts[a])!=null; a++){
				var name = obj.name,
					val = obj.value;
				if(name=='remark3'){continue;}
				if(pp.trim(val) == ""){
					if(!$('.tab-header li:eq(2)').hasClass('s0')){
						$('.tab-header li:eq(2)').click();
					}
					$.messager.alert('操作提示','第【'+ (i+1) +'】行'+khTips[a]+'不能为空！',"warning",function(){
					 	obj.focus();
					});
					return false;
				}
			}
		}
		
		return true;
	};
	return {
		init: function(){
			bindEvent();
			initGasType();
			
			
			<c:forEach items="${details }" var="equ">
				var type = '${equ.device_type}';
				var $tr;
				switch(type){
				case '1':// 气瓶
					$('#qp .btn-add').click();
					$tr = $('#qp tbody tr:last');
					break;
				case '3'://气检仪
					$('#qjy .btn-add').click();
					$tr = $('#qjy tbody tr:last');
					break;
				case '2'://空呼
					$('#kh .btn-add').click();
					$tr = $('#kh tbody tr:last');	
					break;
				}
				
				
				var $text = $tr.find('input,select');
				for(var i=0; i<$text.length; i++){
					switch($text[i].id){
					case'deviceName': $text[i].value='${equ.device_name}'; break;
					case'deviceModel': $text[i].value='${equ.device_model}'; break;
					case'deivceNo': $text[i].value='${equ.device_no}'; break;
					case'deviceManufactor': $text[i].value='${equ.device_manufactor}'; break;
					case'packStatus': $text[i].value='${equ.pack_status}'; break;
					case'lookStatus': $text[i].value='${equ.look_status}'; break;
					case'fileStatus': $text[i].value='${equ.file_status}'; break;
					case'remark': $text[i].value='${equ.remark}'; break;
					case'volumeCategory': $text[i].value='${equ.volume_category}'; break;
					case'manufactureDate': $text[i].value='${equ.manufacture_date}'; break;
					}
					
					if('${view}'=='view'){
						$text[i].disabled = 'disabled';
						var ck = $tr.find(':checkbox');
						if(ck){
							var gasType = '${equ.gas_type}';
							for(var a=0, gas; (gas=ck[a])!=null; a++){
								if(gasType.indexOf(gas.value)!=-1){
									gas.checked = true;
								}
							}
						}
						$('#gasDiv_${i.index+1}').css('min-width','98px');
					}
				} 
			</c:forEach>
		},
		unique: function(){
			return uniqueValid();
		},
		required: function(){
			return requiredValid();
		}
	};
}();

$(function(){
	Index.init();
});
</script>
</html>